package com.example.blog.controller;

import com.example.blog.constants.Constants;
import com.example.blog.model.Result;
import com.example.blog.model.UserInfo;
import com.example.blog.service.UserService;
import com.example.blog.utils.JwtUtils;
import com.example.blog.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/adduser")
    public boolean addUser( String username,String password){

//        password =(SecurityUtils.encrypt(password));

        userService.setUserInfo(username,password);
        return true;
    }

//    @RequestMapping("/adduser")
//    public boolean addUser(@RequestBody UserInfo userInfo){
//        /*
//        * 检验用户名，密码是否为空
//        * 注册用户信息
//        * 返回result
//        * */
//        if(!StringUtils.hasLength(userInfo.getUserName())||!StringUtils.hasLength(userInfo.getPassword())){
//            return false;
//        }
//        if(userInfo.getPassword().length()<7){
//            return false;
//        }
////        进行对密码加密操作
//        userInfo.setPassword(SecurityUtils.encrypt(userInfo.getPassword()));
//
//        userService.setUserInfo(userInfo);
//        return true;
//    }
    @RequestMapping("/login")
    public Result login( String username, String password){

        if(!StringUtils.hasLength(username)||!StringUtils.hasLength(password)){
            return Result.fail("账号密码不能为空") ;

        }
        UserInfo userInfo =userService.selectByName(username);
        if(userInfo ==null){
            return Result.fail("用户不存在");

        }
//        md5加密的密码校验
//        if(!SecurityUtils.verify(password,userInfo.getPassword())){
//            return Result.fail("密码错误");
//        }

        if(!password.equals(userInfo.getPassword())){
            return Result.fail("密码错误");
        }
        Map<String,Object> claims =new HashMap<>();
        claims.put(Constants.TOKEN_ID,userInfo.getId());
        claims.put(Constants.TOKEN_USERNAME,userInfo.getUserName());
        String token = JwtUtils.genJwtToken(claims);
        return Result.success(token);


    }


    
    @RequestMapping("/getUserInfo")
    public UserInfo getLoginInfo(HttpServletRequest request){
//获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);

        //从token 中获取用户id
        Integer userId = JwtUtils.getIdByToken(token);
        if(userId==null){
            return  null;

        }
        UserInfo userInfo = userService.selectById(userId);
        return userInfo;

    }
    @RequestMapping("/getAuthorInfo")
    public UserInfo getAuthorInfo(Integer blogId){

        if(blogId<=0){
            return null;
        }
        UserInfo userInfo = userService.getAuthorInfo(blogId);
        if (userInfo == null) {

            return null;
        }
        return userInfo;

    }




}
